<template>
  <div class="createPost-container">
    <el-form ref="postForm" :model="postForm" :rules="rules" class="form-container">

      <sticky :z-index="10" :class-name="'sub-navbar '+postForm.status">
        <el-button v-loading="loading" style="margin-left: 10px;" type="success" @click="submitForm">
          提交
        </el-button>
        <router-link :to="'/street/streetlist'">
          <el-button class="filter-item" style="margin-left: 10px;" v-loading="loading" type="warning">
            返回
          </el-button>
        </router-link>
      </sticky>

      <div class="createPost-main-container">
        <Warning />
        <el-row>
          <el-col :span="24">
            <div class="postInfo-container">
              <el-row>
                <el-col :span="8">
                  <el-form-item label-width="120px" label="街道名称:" class="postInfo-container-item" prop="name">
                    <el-input v-model="postForm.name"  placeholder="请输入街道名称"></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
            </div>
          </el-col>
        </el-row>
        <el-form-item style="margin-bottom: 40px;" label-width="120px" label="街道负责人:"  prop="personnelid">
          <div style="display:inline-flex">

            <el-select v-model="postForm.personnelid" filterable  placeholder="请选择街道负责人">
              <el-option v-for="item in personnelList" :key="item.id" :label="item.realname" :value="item.id">
              </el-option>
            </el-select>
          </div>
        </el-form-item>
        <el-form-item style="margin-bottom: 40px;" label-width="120px" label="部门负责人:"  prop="departmentid">
          <div style="display:inline-flex">
            <el-select v-model="postForm.departmentid" filterable  placeholder="请选择部门负责人">
              <el-option v-for="item in departmentList" :key="item.id" :label="item.realname" :value="item.id">
              </el-option>
            </el-select>
          </div>
        </el-form-item>

      </div>
    </el-form>
  </div>
</template>

<script>
import Warning from './Warning'
import Sticky from '@/components/Sticky' // 粘性header组件
import { fetchRoad, createRoad, updateRoad, personnelList,departmentList } from '@/api/street/road'

const defaultForm = {
  name: '',
  personnelid: [],
  departmentid: [],
  id: undefined,
}

export default {
  name: 'Detail',
  components: { Sticky, Warning },
  props: {
    isEdit: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      postForm: Object.assign({}, defaultForm),
      loading: false,
      rules: {
        // 规则  required 是否必填  message 规则不通过显示的内容  trigger 触发规则方案 blur 失去焦点时触发
        name: [
          { required: true, message: '请输入街道名称', trigger: 'blur' },
        ],
        personnelid:
         [
          { required: true, message: '请选择街道负责人', trigger: 'blur' },
        ],
         departmentid:
         [
          { required: true, message: '请选择部门负责人', trigger: 'blur' },
        ],
      },
      tempRoute: {},
      personnelList: [],
      departmentList:[],
    }
  },
  computed: {
  },
  created() {
    this.getList()
    // 如果是修改，则获取api的内容
    if (this.isEdit) {
      const id = this.$route.params && this.$route.params.id
      this.fetchData(id)
    }

    this.tempRoute = Object.assign({}, this.$route)
  },
  methods: {
    //获取街道人员
    getList() {
      personnelList().then(response => {
        this.personnelList = response.data
      })
    //获取部门人员
      departmentList().then(response => {
        this.departmentList = response.data
      })
    },
    fetchData(id) {
      fetchRoad(id).then(response => {
        if (JSON.stringify(response.data.Street) != 'null') {
          this.postForm = response.data.Street
        console.log(this.postForm)
        }
      }).catch(err => {
        console.log(err)
      })
    },

    submitForm() {
      this.$refs.postForm.validate(valid => {
        if (valid) {
          this.loading = true
          if (this.isEdit) {
            // this.postForm.personnelid = this.personnelid
            // console.log(this.personnelid)
            updateRoad(this.postForm).then(() => {
              this.$notify({
                title: '成功',
                message: '修改街道成功',
                type: 'success',
                duration: 2000
              })

                this.$router.push('/street/streetlist');
            })
          } else {
            // this.postForm.personnelid = this.personnelid
            // console.log(this.postForm)
            createRoad(this.postForm).then(() => {
              this.$notify({
                title: '成功',
                message: '创建街道成功',
                type: 'success',
                duration: 2000
              })
            })
            // this.$router.push('/street/streetlist');
          }

          this.postForm.status = 'published'
          this.loading = false
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
  }
}
</script>

<style lang="scss" scoped>
@import "~@/styles/mixin.scss";

.createPost-container {
  position: relative;

  .createPost-main-container {
    padding: 40px 45px 20px 50px;

    .postInfo-container {
      position: relative;
      @include clearfix;
      margin-bottom: 10px;

      .postInfo-container-item {
        float: left;
      }
    }
  }

  .word-counter {
    width: 40px;
    position: absolute;
    right: 10px;
    top: 0px;
  }
}

.article-textarea ::v-deep {
  textarea {
    padding-right: 40px;
    resize: none;
    border: none;
    border-radius: 0px;
    border-bottom: 1px solid #bfcbd9;
  }
}
</style>
